{% extends "base.html" %}

{% block title %}图书列表{% endblock %}

{% block content %}
    <div class="p-4 p-md-5 mb-4 text-white rounded bg-secondary">
        <div class="col-md-8 px-0">
            <h1 class="display-4 fst-italic">欢迎来到 MyLibrary</h1>
            <p class="lead my-3">在这里，发现新知，分享智慧。浏览我们丰富的藏书，开启您的阅读之旅。</p>
        </div>
    </div>

    <div class="row">
        <!-- 搜索和筛选区域 -->
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-search me-2"></i>搜索图书</h5>
                </div>
                <div class="card-body">
                    <form method="get" class="row g-3">
                        <div class="col-md-4">
                            {{ search_form.query }}
                        </div>
                        <div class="col-md-2">
                            {{ search_form.category }}
                        </div>
                        <div class="col-md-2">
                            {{ search_form.author }}
                        </div>
                        <div class="col-md-2">
                            <div class="form-check">
                                {{ search_form.available_only }}
                                <label class="form-check-label" for="{{ search_form.available_only.id_for_label }}">
                                    仅显示可借
                                </label>
                            </div>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary w-100">
                                <i class="fas fa-search me-1"></i>搜索
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- 推荐图书区域 -->
        {% if recommendations %}
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-star me-2"></i>为您推荐</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        {% for book in recommendations %}
                        <div class="col-md-2 col-sm-4 col-6 mb-3">
                            <div class="card h-100">
                                {% if book.cover_image %}
                                    <img src="{{ book.cover_image.url }}" class="card-img-top book-cover" alt="{{ book.title }}">
                                {% else %}
                                    <div class="card-img-top book-cover d-flex align-items-center justify-content-center bg-light">
                                        <i class="fas fa-book fa-3x text-muted"></i>
                                    </div>
                                {% endif %}
                                <div class="card-body p-2">
                                    <h6 class="card-title small">{{ book.title|truncatechars:20 }}</h6>
                                    <p class="card-text small text-muted">{{ book.author.name|truncatechars:15 }}</p>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="badge bg-success">{{ book.quantity }}本可借</span>
                                        <a href="{% url 'book_detail' book.pk %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- 热门图书区域 -->
        {% if popular_books %}
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-fire me-2"></i>热门图书</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        {% for book in popular_books %}
                        <div class="col-md-2 col-sm-4 col-6 mb-3">
                            <div class="card h-100">
                                {% if book.cover_image %}
                                    <img src="{{ book.cover_image.url }}" class="card-img-top book-cover" alt="{{ book.title }}">
                                {% else %}
                                    <div class="card-img-top book-cover d-flex align-items-center justify-content-center bg-light">
                                        <i class="fas fa-book fa-3x text-muted"></i>
                                    </div>
                                {% endif %}
                                <div class="card-body p-2">
                                    <h6 class="card-title small">{{ book.title|truncatechars:20 }}</h6>
                                    <p class="card-text small text-muted">{{ book.author.name|truncatechars:15 }}</p>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="badge bg-warning">{{ book.borrow_count }}次借阅</span>
                                        <a href="{% url 'book_detail' book.pk %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- 最新图书区域 -->
        {% if new_books %}
        <div class="col-12 mb-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="fas fa-clock me-2"></i>最新上架</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        {% for book in new_books %}
                        <div class="col-md-2 col-sm-4 col-6 mb-3">
                            <div class="card h-100">
                                {% if book.cover_image %}
                                    <img src="{{ book.cover_image.url }}" class="card-img-top book-cover" alt="{{ book.title }}">
                                {% else %}
                                    <div class="card-img-top book-cover d-flex align-items-center justify-content-center bg-light">
                                        <i class="fas fa-book fa-3x text-muted"></i>
                                    </div>
                                {% endif %}
                                <div class="card-body p-2">
                                    <h6 class="card-title small">{{ book.title|truncatechars:20 }}</h6>
                                    <p class="card-text small text-muted">{{ book.author.name|truncatechars:15 }}</p>
                                    <div class="d-flex justify-content-between align-items-center">
                                        <span class="badge bg-info">新书</span>
                                        <a href="{% url 'book_detail' book.pk %}" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- 所有图书列表 -->
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-books me-2"></i>所有图书</h5>
                    <span class="badge bg-primary">{{ page_obj.paginator.count }}本图书</span>
                </div>
                <div class="card-body">
                    {% if books %}
                    <div class="row">
                        {% for book in books %}
                        <div class="col-lg-3 col-md-4 col-sm-6 mb-4">
                            <div class="card h-100">
                                {% if book.cover_image %}
                                    <img src="{{ book.cover_image.url }}" class="card-img-top book-cover" alt="{{ book.title }}">
                                {% else %}
                                    <div class="card-img-top book-cover d-flex align-items-center justify-content-center bg-light">
                                        <i class="fas fa-book fa-3x text-muted"></i>
                                    </div>
                                {% endif %}
                                <div class="card-body">
                                    <h6 class="card-title">{{ book.title }}</h6>
                                    <p class="card-text text-muted small">
                                        <i class="fas fa-user me-1"></i>{{ book.author.name }}
                                    </p>
                                    {% if book.category %}
                                    <span class="category-badge" style="background-color: {{ book.category.color }};">
                                        {{ book.category.name }}
                                    </span>
                                    {% endif %}
                                    <div class="mt-2">
                                        {% if book.average_rating > 0 %}
                                        <div class="rating-stars mb-2">
                                            {% for i in "12345" %}
                                                {% if forloop.counter <= book.average_rating %}
                                                    <i class="fas fa-star"></i>
                                                {% else %}
                                                    <i class="far fa-star"></i>
                                                {% endif %}
                                            {% endfor %}
                                            <span class="ms-1 text-muted small">{{ book.average_rating|floatformat:1 }}</span>
                                        </div>
                                        {% endif %}
                                        <div class="d-flex justify-content-between align-items-center">
                                            {% if book.quantity > 0 %}
                                                <span class="badge bg-success">
                                                    <i class="fas fa-check me-1"></i>{{ book.quantity }}本可借
                                                </span>
                                            {% else %}
                                                <span class="badge bg-danger">
                                                    <i class="fas fa-times me-1"></i>已借完
                                                </span>
                                            {% endif %}
                                            <div class="btn-group" role="group">
                                                <a href="{% url 'book_detail' book.pk %}" class="btn btn-sm btn-outline-primary">
                                                    <i class="fas fa-eye me-1"></i>详情
                                                </a>
                                                {% if user.is_authenticated and book.quantity > 0 %}
                                                <a href="{% url 'borrow_book' book.pk %}" class="btn btn-sm btn-success">
                                                    <i class="fas fa-bookmark me-1"></i>借阅
                                                </a>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>

                    <!-- 分页 -->
                    {% if is_paginated %}
                    <nav aria-label="图书分页" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?page=1{% if request.GET.query %}&query={{ request.GET.query }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.author %}&author={{ request.GET.author }}{% endif %}{% if request.GET.available_only %}&available_only=on{% endif %}">
                                        <i class="fas fa-angle-double-left"></i>
                                    </a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if request.GET.query %}&query={{ request.GET.query }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.author %}&author={{ request.GET.author }}{% endif %}{% if request.GET.available_only %}&available_only=on{% endif %}">
                                        <i class="fas fa-angle-left"></i>
                                    </a>
                                </li>
                            {% endif %}

                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?page={{ num }}{% if request.GET.query %}&query={{ request.GET.query }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.author %}&author={{ request.GET.author }}{% endif %}{% if request.GET.available_only %}&available_only=on{% endif %}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}

                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if request.GET.query %}&query={{ request.GET.query }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.author %}&author={{ request.GET.author }}{% endif %}{% if request.GET.available_only %}&available_only=on{% endif %}">
                                        <i class="fas fa-angle-right"></i>
                                    </a>
                                </li>
                                <li class="page-item">
                                    <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if request.GET.query %}&query={{ request.GET.query }}{% endif %}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.author %}&author={{ request.GET.author }}{% endif %}{% if request.GET.available_only %}&available_only=on{% endif %}">
                                        <i class="fas fa-angle-double-right"></i>
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-search fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">没有找到相关图书</h5>
                        <p class="text-muted">请尝试调整搜索条件</p>
                        <a href="{% url 'user_book_list' %}" class="btn btn-primary">
                            <i class="fas fa-home me-1"></i>返回首页
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}